//
//  WelcomeViewController.swift
//  SinaWeibo
//
//  Created by 梁楠 on 16/1/17.
//  Copyright © 2016年 QQ主流框架. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    override func loadView() {
        view = backImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    //动画效果不推荐在ViewDidLoad / loadView 中执行
    //推荐在ViewDidAppear 中执行
    
    override func viewDidAppear(animated: Bool) {
        
        showAnimation()
    }
    
    private func setupUI () {
        
        self.view.addSubview(headImageView)
        self.view.addSubview(welcomeLabel)
        
        headImageView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            make.centerX.equalTo(self.view.snp_centerX)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            
            make.centerX.equalTo(headImageView.snp_centerX)
        }
        
        //头像的圆角设置
        headImageView.layer.cornerRadius = 45
        headImageView.layer.masksToBounds = true
        
        welcomeLabel.alpha = 0
        
        headImageView.sd_setImageWithURL(UserAccountViewModel().headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
    //MARK 显示动画效果
    private func showAnimation() {
        
        let offset = -UIScreen.mainScreen().bounds.height + 160
        
        //usingSpringWithDamping : 弹簧系数 0.1~1 越小越弹球
        //弹簧系数设置准则 (弹簧系数 * 10 ~= 加速度)
        //initialSpringVelocity : 加速度
        //options : 动画可选项 OC中按位枚举  swift: 数组
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            //更新某一条已经添加过得约束,如果该约束不存在,会自动添加该约束
            self.headImageView.snp_updateConstraints { (make) -> Void in
                make.bottom.equalTo(self.view.snp_bottom).offset(offset)
            
            }
            //使用自动布局 + 动画闭包 ===> 强制刷新视图
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVC, object: nil)
                        
                        print("通知")
                })
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    private lazy var backImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var headImageView : UIImageView = UIImageView(image: UIImage(named:""))
    
    private lazy var welcomeLabel : UILabel = {
       
        let l = UILabel()
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(18)
        l.text = (UserAccountViewModel().userName ?? "") + " 欢迎归来"
        l.sizeToFit()
        return l
    }()

}
